import React from 'react';
import ReactDOM from 'react-dom';
import LoadingCMP from './Loading';

// let component = null;
let dom = null;

// const show = (props) => {
//   component = document.createElement('div');
//   document.body.appendChild(component);

//   // 删除方法
//   const close = () => {
//     if (component) {
//       ReactDOM.unmountComponentAtNode(component);
//       document.body.removeChild(component);
//     }
//   }

//   dom = ReactDOM.render((<LoadingCMP show={true} close={close}></LoadingCMP>), component);
// };

// const hide = () => {
//   dom.close();
// };

const show = (props) => {
  let div = document.createElement('div');
  document.body.appendChild(div);

  const close = () => {
    if (div) {
      ReactDOM.unmountComponentAtNode(div);
      document.body.removeChild(div);
    }
  }

  dom = ReactDOM.render(<LoadingCMP show={true} close={close}></LoadingCMP>, div);
};

const hide = () => {
  dom.close();
};

const Loading = {};
Loading.show = show;
Loading.hide = hide;

export default Loading;